<?php
namespace app\madmin\model;
use think\Model;

/**
 * 微信菜单模型类
 * @author  黄明杰 vx:HMJ125855
 * @version 20190101
 */
class WechatMenuModel extends Model
{

    protected $name = 'WechatMenu';

    /**
     * 数据列表方法
     * @param   Array       $Where      查询条件
     * @param   String      $Field      查询字段
     * @param   Srting      $Order      查询排序
     * @return  Array
     */
    public function mList($Where = [], $Field = '*', $Order = 'menu_id ASC'){
        $Where['menu_del'] = 1;

        return $this->field($Field)->where($Where)->order($Order)->select();
    }

    /**
     * 数据总数方法
     * @param   Array       $Where      查询条件
     * @return  Int
     */
    public function mCount($Where = []){
        $Where['menu_del'] = 1;

        return $this->where($Where)->count();
    }

    /**
     * 数据详情方法
     * @param   Array       $Where      查询条件
     * @param   String      $Field      查询字段
     * @return  Array
     */
    public function mInfo($Where = [], $Field = '*'){
        $Where['menu_del'] = 1;

        return $this->field($Field)->where($Where)->find();
    }

    /**
     * 数据详情方法
     * @param   Array       $Where      查询条件
     * @param   String      $Field      查询字段
     * @return  String
     */
    public function mField($Where, $Field){
        $Where['menu_del'] = 1;

        return $this->where($Where)->value($Field);
    }

    /**
     * 数据新增方法
     * @param   Array       $Param      添加内容
     * @return  Array
     */
    public function mAdd($Param){
        $Code = 2;
        $Time = time();

        try{
            $Param['menu_create'] = $Time;

            $Result = $this->allowField(true)->insert($Param);

            $AddID = $this->getLastInsID();

            if($Result){
                $Code = 1;
                $Msgs = lang('XtTsTjCg');
            }else{
                $Msgs = lang('XtTsTjSb');
            }

            addMemberLog(session('AdminID'), 2, lang('WxCdRzXz', ['m'=>session('AdminUser'), 'd'=>$AddID]), $Code);

            return ['code'=>$Code, 'msgs'=>$Msgs, 'urls'=>url('WechatMenu/showList')];
        }catch(PDOException $e){
            return ['code'=>$Code, 'msgs'=>$e->getMessage()];
        }
    }

    /**
     * 数据更新方法
     * @param   Array       $Where      更新条件
     * @param   Array       $Param      更新内容
     * @return  Array
     */
    public function mEdit($Where, $Param){
        $Code = 2;
        $Time = time();

        try{
            $Find = $this->mInfo($Where);

            if(empty($Find)){
                $Msgs = lang('XtCwCxCw');

                return ['code'=>$Code, 'msgs'=>$Msgs];
            }else{
                $Where['menu_del'] = 1;

                $Param['menu_update'] = $Time;

                $Result = $this->allowField(true)->where($Where)->update($Param);

                if($Result){
                    $Code = 1;
                    $Msgs = lang('XtTsGxCg');
                }else{
                    $Msgs = lang('XtTsGxSb');
                }
     
                addMemberLog(session('AdminID'), 3, lang('WxCdRzGx', ['m'=>session('AdminUser'), 'd'=>$Where['menu_id']]), $Code);

                return ['code'=>$Code, 'msgs'=>$Msgs, 'urls'=>url('WechatMenu/showList')];
            }
        }catch(PDOException $e){
            return ['code'=>$Code, 'msgs'=>$e->getMessage()];
        }
    }

    /**
     * 数据删除方法
     * @param   Array       $Where      更新条件
     * @return  Array
     */
    public function mDel($Where){
        $Code = 2;
        $Time = time();

        try{
            $Find = $this->mInfo($Where);

            if(empty($Find)){
                $Msgs = lang('XtCwCxCw');

                return ['code'=>$Code, 'msgs'=>$Msgs];
            }else{
                $Where['menu_del'] = 1;

                $Param = [];
                $Param['menu_del'] = 2;
                $Param['menu_update'] = $Time;

                $Result = $this->allowField(true)->where($Where)->update($Param);

                if($Result){
                    $Code = 1;
                    $Msgs = lang('XtTsScCs');
                }else{
                    $Msgs = lang('XtTsScSb');
                }

                addMemberLog(session('AdminID'), 4, lang('WxCdRzSc', ['m'=>session('AdminUser'), 'd'=>$Where['menu_id']]), $Code);
     
                return ['code'=>$Code, 'msgs'=>$Msgs, 'urls'=>url('WechatMenu/showList')];
            }
        }catch(PDOException $e){
            return ['code'=>$Code, 'msgs'=>$e->getMessage()];
        }
    }

    /**
     * 数据生成方法
     * @param   Array       $Access     验证数组
     * @param   Array       $Menu       菜单数组
     * @return  Array
     */
    public function mMake($Access, $Menu){
        $Code = 2;
        $Time = time();

        try{
            if(!empty($Access)){
                $Urls = 'https://api.weixin.qq.com/cgi-bin/menu/create?access_token='.$Access['access_token'];

                $Data = [];
                $Data['button'] = $Menu;

                $Back = CurlPost($Urls, json_encode($Data, JSON_UNESCAPED_UNICODE));

                if($Back['errcode'] == 0){
                    $Code = 1;
                    $Msgs = lang('WxCdZdyCj1');
                }else{
                    $Msgs = lang('WxCdZdyCj2');
                }
            }else{
                $Msgs = lang('WxAtCw');
            }

            addMemberLog(session('AdminID'), 2, lang('WxCdRzZdyCj', ['m'=>session('AdminUser')]), $Code);

            return ['code'=>$Code, 'msgs'=>$Msgs];
        }catch(PDOException $e){
            return ['code'=>$Code, 'msgs'=>$e->getMessage()];
        }
    }

    /**
     * 数据删除方法
     * @param   Array       $Access     验证数组
     * @return  Array
     */
    public function mDele($Access){
        $Code = 2;
        $Time = time();

        try{
            if(!empty($Access)){
                $Urls = 'https://api.weixin.qq.com/cgi-bin/menu/delete?access_token='.$Access['access_token'];

                $Back = CurlGet($Urls);

                if($Back['errcode'] == 0){
                    $Code = 1;
                    $Msgs = lang('WxCdZdySc1');
                }else{
                    $Code = 2;
                    $Msgs = lang('WxCdZdySc2');
                }
            }else{
                $Code = 2;
                $Msgs = lang('WxAtCw');
            }

            addMemberLog(session('AdminID'), 4, lang('WxCdRzZdySc', ['m'=>session('AdminUser')]), $Code);

            return ['code'=>$Code, 'msgs'=>$Msgs];
        }catch(PDOException $e){
            return ['code'=>$Code, 'msgs'=>$e->getMessage()];
        }
    }

    /**
     * 数据所有方法
     * @param   Array       $Menu       操作数组
     * @return  Array
     */
    public static function mMenu($Menu, $Pid = 0){
        $Array = [];

        foreach($Menu as $k => $v){
            if($v['menu_pid'] == $Pid){
                $Array[$k] = $v;

                $Child = self::mMenu($Menu, $v['menu_id']);

                $Array[$k]['menu_child'] = $Child;
            }
        }

        return array_values($Array);
    }

    /**
     * 数据排序方法
     * @param   Array       $Class      操作数组
     * @return  Array
     */
    public static function mTree($Class, $Line = '┗━', $Pid = 0, $Level = 0, $Left = 50){
        $Array = [];

        foreach($Class as $v){
            if($v['menu_pid'] == $Pid){
                $v['menu_level'] = $Level + 1;
                $v['menu_left'] = $Left + 0;
                $v['menu_line'] = str_repeat($Line, $Level);

                $Array[] = $v;
                $Array = array_merge($Array, self::mTree($Class, $Line, $v['menu_id'], $Level + 1 , $Left + 20));
            }
        }

        return $Array;
    }

}